package com.designpattern.design.observer.improve;

import java.util.ArrayList;
import java.util.List;

public class Observertest {
public static void main(String[] args) {
	SinaObserver observer = new SinaObserver();
	List<Observer> list = new ArrayList<Observer>();
	list.add(observer);
	WeatherData data = new WeatherData(list);
	data.setData(1f, 2f, 3f);
	//如果要扩展 那么很简单新建一个类 add到list里面去
	YahooObserver observer2 = new YahooObserver();
	data.registerObserver(observer2);
	data.setData(3f, 4f, 5f);
	
}
}

/**
 * 观察方接口
 * @author 15790
 *
 */
interface WeatherInterface{
	void registerObserver(Observer obs);
	void removeObserver(Observer obs);
	void nodifyObservers();
}
/**
 * 观察者实例 聚合多个接收方
 * @author 15790
 *
 */
class WeatherData implements WeatherInterface{
	
	private float temperature;
	private float pressure;
	private float humidity;
	//接收方集合
	List<Observer> observers;
	
	
	public WeatherData(List<Observer> list) {
		super();
		this.observers = list;
	}
	
	
	public float getTemperature() {
		return temperature;
	}


	public void setTemperature(float temperature) {
		this.temperature = temperature;
	}


	public float getPressure() {
		return pressure;
	}


	public void setPressure(float pressure) {
		this.pressure = pressure;
	}


	public float getHumidity() {
		return humidity;
	}


	public void setHumidity(float humidity) {
		this.humidity = humidity;
	}
	


	public List<Observer> getObservers() {
		return observers;
	}


	public void setObservers(List<Observer> observers) {
		this.observers = observers;
	}


	// 传入最新的天气数据
	public void setData(float temperature, float pressure, float humidity) {
		this.temperature = temperature;
		this.pressure = pressure;
		this.humidity = humidity;
		this.nodifyObservers();
	}
	
	
	@Override
	public void registerObserver(Observer obs) {
		// TODO Auto-generated method stub
		observers.add(obs);
	}

	@Override
	public void removeObserver(Observer obs) {
		// TODO Auto-generated method stub
		observers.remove(obs);
	}

	@Override
	public void nodifyObservers() {
		// TODO Auto-generated method stub
		for(Observer obs:observers) {
			obs.update(temperature, pressure, humidity);
		}
	}
	
}

/**
 * 接收方接口
 * @author 15790
 *
 */
interface Observer{
	void update(float temperature,float pressure,float humidity);
}

/**
 * 接收方实例-新浪
 * @author 15790
 *
 */
class SinaObserver implements Observer{
	private float temperature;
	private float pressure;
	private float humidity;
	@Override
	public void update(float temperature, float pressure, float humidity) {
		// TODO Auto-generated method stub
		this.temperature = temperature;
		this.pressure = pressure;
		this.humidity = humidity;
		display();
	}
	
	public void display() {
		System.out.println("气温=" + this.temperature);
		System.out.println("气压=" + this.pressure);
		System.out.println("湿度=" + this.humidity);
	}
	
}

/**
 * 接收方实例-雅虎
 * @author 15790
 *
 */
class YahooObserver implements Observer{
	private float temperature;
	private float pressure;
	private float humidity;
	@Override
	public void update(float temperature, float pressure, float humidity) {
		// TODO Auto-generated method stub
		this.temperature = temperature;
		this.pressure = pressure;
		this.humidity = humidity;
		display();
	}
	
	public void display() {
		System.out.println("雅虎气温=" + this.temperature);
		System.out.println("雅虎气压=" + this.pressure);
		System.out.println("雅虎湿度=" + this.humidity);
	}
	
}